package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {
    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;
    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //传进来的参数只是一个分类id，base_trademark和category3Id没有关系
        //    base_category_trademark 这张表中有category3_id，通过分类id可以获取品牌id
        //通过品牌id能够获取base_trademark的数据
        //就是做了个关联查询
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        //所有对象的集合
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);
//        List<Long> longIdList = new ArrayList<>();
//        for (BaseCategoryTrademark baseCategoryTrademark : baseCategoryTrademarkList) {
//            longIdList.add(baseCategoryTrademark.getTrademarkId());
//        }
        //用拉姆达表达式
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            List<Long> longIdList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            return baseTrademarkMapper.selectBatchIds(longIdList);
        }
        return null;

    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        //所有对象的集合
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //获取当前分类id对应的品牌id
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            List<BaseTrademark> BaseTrademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> {
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
            return BaseTrademarkList;
        }
        //没有获取到数据加载所有品牌数据

        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void saveCategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        //就是向base_category_trademark插入数据
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();

//        for (Long tmId : trademarkIdList) {
//            Long category3Id = categoryTrademarkVo.getCategory3Id();
//            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
//            baseCategoryTrademark.setCategory3Id(category3Id);
//            baseCategoryTrademark.setTrademarkId(tmId);
//            //插入数据：循环插入数据
//            baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
//        }
//        List<BaseCategoryTrademark> baseCategoryTrademarks=new ArrayList<>();
//        for (Long tmId : trademarkIdList) {
//            Long category3Id = categoryTrademarkVo.getCategory3Id();
//            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
//
//            baseCategoryTrademark.setCategory3Id(category3Id);
//            baseCategoryTrademark.setTrademarkId(tmId);
//            baseCategoryTrademarks.add(baseCategoryTrademark);
//        }
        //map 映射
        List<BaseCategoryTrademark> baseCategoryTrademarks = trademarkIdList.stream().map(tmId -> {
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();

            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(tmId);
            return baseCategoryTrademark;
        }).collect(Collectors.toList());
        //批量插入数据

        this.saveBatch(baseCategoryTrademarks);
    }

    @Override
    public void removebaseCategoryTrademark(Long category3Id, Long trademarkId) {
       // base_category_trademark 解除绑定
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper=new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id",trademarkId);
        baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);
    }
}
